from selenium.webdriver.support.wait import WebDriverWait
import time
import page
class Base:

    def __init__(self, driver):
        self.driver = driver

    # 查找元素
    def base_find(self, loc, timeout=30, poll=0.5):
        return WebDriverWait(self.driver, timeout=timeout,
                             poll_frequency=poll).until(lambda x: x.find_element(*loc))
    # 点击元素
    def base_click(self, loc):
        self.base_find(loc).click()

    # 输入元素
    def base_input(self, loc, value):
        ele = self.base_find(loc)
        ele.clear()
        ele.send_keys(value)

    # 获取文本信息
    def base_get_text(self, loc):
        return self.base_find(loc).text

    # 截图
    def base_image(self):
        self.driver.get_screenshot_as_file("../images/{}.png".format(time.strftime("%Y_%m_%d %H_%M_%S")))

    # 判断元素是否存在
    def base_element_is_exist(self, loc):
        try:
            self.base_find(loc)
            return True
        except:
            return False
    # 切换frame表单
    def base_switch_frame(self, name):
        self.driver.switch_to.frame(name)

    # 回到默认目录方法
    def base_default_content(self):
        self.driver.switch_to.default_content()

    # 回到首页
    def base_index(self):
        time.sleep(2)
        self.driver.get(page.URL)

    # 切换窗口 方法 调用此方法
    def base_switch_to_window(self, title):
        self.driver.switch_to.window(self.base_get_title_handle(title))

    # 获取指定title页面的handle方法
    def base_get_title_handle(self, title):
        # 获取当前页面所有的handles
        for handle in self.driver.window_handles:
            # 切换 handle
            self.driver.switch_to.window(handle)
            # 获取当前页面title 并判断 是否等于 指定参数title
            if self.driver.title == title:
                # 返回 handle
                return handle